Optimal Time and Space Complexity Algorithm for Construction of All Binary Trees from Pre-order and Post-order Traversals

نویسنده

  • ADRIAN DEACONU
چکیده

A linear time and space algorithm for construction of a binary tree from the pre-order and post-order traversals is presented. The solution is not always unique. The number of solutions is calculated and an optimal time and space method to find all the solutions is shown. Keywords: binary tree, traversals, construction of tree 1. INTRODUCTION The problem of construction the binary tree from the pre-order (or post-order) and in-order traversals is well-known [1][2][3][4][5]. The linear time and space was achieved. The case of pre-order and post-order is less studied [5][6]. The solution in this case is not even unique. We shall present an algorithm to find a solution in linear time and space and, starting from this one, we’ll be able to find all the solutions in optimal time and space complexity. The method in this paper for finding one solution is adapted from the algorithm for finding the tree from pre-order and post-order traversal vectors [6]. All the notations we shall use are according to the textbook of Ahuja, Magnanti and Orlin [7]. It is well-known the following propriety for the post-order traversal vector: Propriety 1 The reversed post-order vector is a root-right-to-left traversal vector. We shall name the root-left-to-right traversal as first pre-order traversal and the root-right-to-left traversal as the second pre-order traversal, because the nodes are analyzed similarly to pre-order traversal: root and then it’s successors, but in reversed order (right-to-left). The 7 Balkan Conference on Operational Research “BACOR 05” Constanta, May 2005, Romania In this paper we shall refer to the first and the second pre-order traversal vectors as the pre-order (traversal) vectors of the trees. It is easy to observe that a pre-order (first or second) traversal vector starts with the root. So, we have the following propriety: Propriety 2 In a (first or second) pre-order traversal vector the first element is the root of the binary tree. Let (ai)iN be the first pre-order traversal vector and (bi)iN the second pre-order traversal vector. We suppose for each i and j  {1, 2, …, n}, i  j that ai  aj and bi  bj. It is obviously that: { ai | i = 1, 2, …, n } = { bj | j = 1, 2, …, n } = N, (1.1) where N is the set of nodes of the binary tree, |N| = n. The following propriety of the first pre-order traversal vector helps us to prove the main theorems of this paper (theorem 1 and theorem 2). Propriety 3 If u and v are consecutive nodes in the first pre-order traversal vector, then we have one of the following situations: i) v is a son of the node u (see fig. 1.1). If u has two sons, then v is the left one. or: ii) v is the son to the right of the node w and u is the last node in the first pre-order traversal vector of the left partial tree T1 of w (see fig. 1.2). 2. FINDING A SOLUTION The algorithm we shall present is a adapted from the algorithm for reconstruction of the tree from the pre-order and post-order traversals [6]. We shall prove two theorems, which will lead us to the method to construct a binary tree from the pre-order and post-order traversals. For each consecutive pair of nodes, u and v, from the first pre-order traversal vector using one of the next two theorems we can find the father denoted w of the node v. The method of finding w involves one of the situations described in propriety 3. The main idea is to find a criterion for each situation that can appear and then to prove that there is only one node with this propriety. u

برای دانلود رایگان متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

An improved algorithm to reconstruct a binary tree from its inorder and postorder traversals

It is well-known that, given inorder traversal along with one of the preorder or postorder traversals of a binary tree, the tree can be determined uniquely. Several algorithms have been proposed to reconstruct a binary tree from its inorder and preorder traversals. There is one study to reconstruct a binary tree from its inorder and postorder traversals, and this algorithm takes running time of...

متن کامل

An improved algorithm to reconstruct a binary tree from its inorder and postorder traversals

It is well-known that, given inorder traversal along with one of the preorder or postorder traversals of a binary tree, the tree can be determined uniquely. Several algorithms have been proposed to reconstruct a binary tree from its inorder and preorder traversals. There is one study to reconstruct a binary tree from its inorder and postorder traversals, and this algorithm takes running time of...

متن کامل

A novel algorithm to determine the leaf (leaves) of a binary tree from its preorder and postorder traversals

Binary trees are essential structures in Computer Science. The leaf (leaves) of a binary tree is one of the most significant aspects of it. In this study, we prove that the order of a leaf (leaves) of a binary tree is the same in the main tree traversals; preorder, inorder, and postorder. Then, we prove that given the preorder and postorder traversals of a binary tree, the leaf (leaves) of a bi...

متن کامل

Parallel Generation of t-ary Trees

A parallel algorithm for generating t-ary tree sequences in reverse B-order is presented. The algorithm generates t-ary trees by 0-1 sequences, and each 0-1 sequences is generated in constant average time O(1). The algorithm is executed on a CREW SM SIMD model, and is adaptive and cost-optimal. Prior to the discussion of the parallel algorithm a new sequential generation with O(1) average time ...

متن کامل

Iterative Method for Recreating a Binary Tree from its Traversals

Many reconstruction algorithms for binary tree have been discussed in this paper. A particular focus of this paper is on “A new Non-Recursive Algorithm for Reconstructing a Binary Tree from its Traversals”. The computation time required for executing the reconstruction algorithm are O(N) and space complexity is O(NlogN) where N is the number of nodes in the binary tree. This algorithm works wel...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 2006